home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2a.lha / p4-1.2a / lib / p4_sock_list.c < prev    next >
C/C++ Source or Header  |  1992-11-19  |  3KB  |  150 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. P4VOID listener()
  5. {
  6.     struct listener_data *l = listener_info;
  7.     P4BOOL done = FALSE;
  8.     fd_set read_fds;
  9.     int i, nfds, fd;
  10.  
  11.     p4_dprintfl(70, "enter listener \n");
  12.     dump_listener(70);
  13.  
  14.     while (!done)
  15.     {
  16.     FD_ZERO(&read_fds);
  17.     FD_SET(l->listening_fd, &read_fds);
  18.     FD_SET(l->slave_fd, &read_fds);
  19.  
  20.     SYSCALL_P4(nfds, select(p4_global->max_connections, &read_fds, 0, 0, 0));
  21.     if (nfds < 0)
  22.         p4_error("listener select", nfds);
  23.     if (nfds == 0)
  24.         p4_dprintfl(70, "select timeout\n");
  25.  
  26.     fd = 0;
  27.     for (i = 0; i < nfds && !done; i++)
  28.     {
  29.         while (fd < p4_global->max_connections)
  30.         {
  31.         if (FD_ISSET(fd, &read_fds))
  32.         {
  33.             if (fd == l->listening_fd || fd == l->slave_fd)
  34.             break;
  35.         }
  36.         fd++;
  37.         }
  38.  
  39.         p4_dprintfl(70, "got fd=%d listening_fd=%d slave_fd=%d\n",
  40.             fd, l->listening_fd, l->slave_fd);
  41.  
  42.         if (fd == l->listening_fd)
  43.         done = process_connect_request(fd);
  44.         else
  45.         done = process_slave_message(fd);
  46.         fd++;
  47.     }
  48.     }
  49.  
  50.     p4_dprintfl(70, "exit listener\n");
  51.     exit(0);
  52. }
  53.  
  54. P4BOOL process_connect_request(fd)
  55. int fd;
  56. {
  57.     struct slave_listener_msg msg;
  58.     int type;
  59.     int connection_fd, slave_fd;
  60.     int from, lport, to_pid, to;
  61.     P4BOOL rc = FALSE;
  62.  
  63.     p4_dprintfl(70, "processing connect check/request on %d\n", fd);
  64.  
  65.     connection_fd = net_accept(fd);
  66.  
  67.     p4_dprintfl(70, "accepted on connection_fd=%d reading size=%d\n", connection_fd,sizeof(msg));
  68.  
  69.     if (net_recv(connection_fd, &msg, sizeof(msg)) == PRECV_EOF)
  70.     {
  71.     return (FALSE);
  72.     }
  73.  
  74.     type = p4_n_to_i(msg.type);
  75.     switch (type)
  76.     {
  77.       case IGNORE_THIS:
  78.     p4_dprintfl(70, "got IGNORE_THIS\n");
  79.     break;
  80.  
  81.       case CONNECTION_REQUEST:
  82.     from = p4_n_to_i(msg.from);
  83.     to_pid = p4_n_to_i(msg.to_pid);
  84.     to = p4_n_to_i(msg.to);
  85.     lport = p4_n_to_i(msg.lport);
  86.     p4_dprintfl(70, "connection_request2: poking slave: from=%d lport=%d to_pid=%d to=%d\n",
  87.             from, lport, to_pid, to);
  88.  
  89.     slave_fd = listener_info->slave_fd;
  90.  
  91.     if (kill(to_pid, LISTENER_ATTN_SIGNAL) == -1)
  92.     {
  93.         p4_dprintf("Listener: Unable to interrupt client pid=%d.\n", to_pid);
  94.         break;
  95.     }
  96.  
  97.     net_send(slave_fd, &msg, sizeof(msg), FALSE);
  98.     /* wait for msg from slave indicating it got connected */
  99.     /*
  100.      * do not accept any more connections for slave until it has fully
  101.      * completed this one, i.e. do not want to interrupt it until it has
  102.      * handled this interrupt
  103.      */
  104.     p4_dprintfl(70, "waiting for slave to handle interrupt\n");
  105.     net_recv(slave_fd, &msg, sizeof(msg));
  106.     p4_dprintfl(70, "back from slave handling interrupt\n");
  107.     break;
  108.  
  109.       default:
  110.     p4_dprintf("invalid type %d in process_connect_request\n", type);
  111.     break;
  112.     }
  113.     close(connection_fd);
  114.     return (rc);
  115. }
  116.  
  117. P4BOOL process_slave_message(fd)
  118. int fd;
  119. {
  120.     struct slave_listener_msg msg;
  121.     int type;
  122.     int from;
  123.     P4BOOL rc = FALSE;
  124.     int status;
  125.  
  126.     status = net_recv(fd, &msg, sizeof(msg));
  127.     if (status == PRECV_EOF)
  128.     {
  129.     p4_error("slave_listener_msg: got eof on fd=", fd);
  130.     }
  131.  
  132.     type = p4_n_to_i(msg.type);
  133.     from = p4_n_to_i(msg.from);
  134.  
  135.     switch (type)
  136.     {
  137.       case DIE:
  138.     p4_dprintfl(70, "received die msg from %d\n", from);
  139.     rc = TRUE;
  140.     break;
  141.  
  142.       default:
  143.     p4_dprintf("received unknown message type=%d from=%d\n", type, from);
  144.     p4_error("slave_listener_msg: unknown message type", type);
  145.     break;
  146.     }
  147.  
  148.     return (rc);
  149. }
  150.